#include <vector>
#include <queue>
using namespace std;

class Solution
{
public:
    static int lastStoneWeight(vector<int>& stones)
    {
        priority_queue<int> heap;
        for(auto e : stones)
        {
            heap.push(e);
        }
        while(heap.size() >= 2)
        {
            int x = heap.top();
            heap.pop();
            int y = heap.top();
            heap.pop();
            if(x != y)
                heap.push(x-y);
        }
        return heap.empty() ? 0 : heap.top();
    }
};
